/**
 * 上传页面
 * @author yuan_rz
 * @date 2019/02/19 16:10
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            files: g_ctxPath + 'upload/files',
            fileList: [],
            form: {
                desc: ''
            },
            data:{
            }
        },
        cb,
        beforeUploadFiles,
        successUploadFiles,
        handleFileRemove,
        handlePreview
    ;

    app.newModule({
        init: function () {
            self = this;
            self._super({
                _tag: 'common_open_upload'
            }, win);
        },
        _call: function (tagData) {
            //上传文件URL地址
            if(tagData.url){
                vueData.files = g_ctxPath + tagData.url;
            }
            //文件列表
            if(tagData.fileList){
                vueData.fileList = tagData.fileList
            }else{
                vueData.fileList = [];
            }
            //上传额外参数
            vueData.data = tagData.data;
            //点击确认时的回调
            cb = tagData.cb;
            //上传之前的检验
            beforeUploadFiles = tagData.beforeUpload;
            //成功上传的回调函数
            successUploadFiles = tagData.onSuccess;
            //删除时调用的方法
            handleFileRemove = tagData.onRemove;
            //点击时调用的方法
            handlePreview = tagData.onPreview;
        },
        _ready: function () {
            var vue = new Vue({
                el: '#common_open_upload',
                data: vueData,
                methods: {
                    cancel: function () {
                        self._closeTpl();
                    },
                    sure: function () {
                        if(cb){
                            cb(vueData.fileList);
                        }
                        self._closeTpl();
                    },
                    beforeUpload: function (file) {
                        if(beforeUploadFiles){
                            beforeUploadFiles(file);
                        }
                    },
                    onSuccess: function (response,file, fileList) {
                        vueData.fileList = fileList;
                        if(successUploadFiles){
                            successUploadFiles(response,file, fileList);
                        }
                    },
                    onRemove: function (file, fileList) {
                        vueData.fileList = fileList;
                        if(handleFileRemove){
                            handleFileRemove(file,fileList);
                        }
                    },
                    onPreview: function (file) {
                        if(handlePreview){
                            handlePreview(file);
                        }
                    }
                },
                created: function () {
                    self._setEleIsReady('common_open_upload');
                }
            });
        },
        _lazy: function () {
            this._super();
        }
    });

});